﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Yarp.ReverseProxy.Configuration;

namespace Quickly.SpaProxy.ReverseProxy;

public static class SpaReverseProxyConfigurationBuilderExtensions
{
    public static IReverseProxyBuilder LoadFromSpaProxy(this IReverseProxyBuilder builder, IConfiguration configuration)
    {
        ArgumentNullException.ThrowIfNull(configuration, nameof(configuration));

        builder.Services.AddSingleton<IProxyConfigProvider>(sp =>
        {
            return new SpaReverseProxyConfigProvider(sp.GetRequiredService<ILogger<SpaReverseProxyConfigProvider>>(), configuration);
        });

        return builder;
    }
}
